//
//  SettlementDetailsController.m
//  ios-supplier
//
//  Created by Benlai on 15/8/30.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "SettlementDetailsController.h"
#import "SettlementDetailsCell.h"
#import "OrderSettlementController.h"
#import "JFBSettlementDetailsDS.h"
#import "JFBLoadingAnimation.h"
#import "MJRefresh.h"


@interface SettlementDetailsController () <UITableViewDataSource, UITableViewDelegate,UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (nonatomic, strong) NSMutableArray *items;
//@property (nonatomic ,strong) NSDictionary *aaa;

@end

@implementation SettlementDetailsController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"name is %ld", (long)_ID);
    self.tableView.tableHeaderView = self.headerView;
    self.items = [@[] mutableCopy];
    self.tableView.bounces = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
//    [self.tableView setSeparatorStyle: UITableViewCellSeparatorStyleNone];
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerLoadNewData)];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
    [self reload:nil];
}

-(void)headerLoadNewData
{
    [JFBSettlementDetailsDS loadSettlementDetail:^(NSArray *statisticsList, NSError *error) {
        self.items = [NSMutableArray arrayWithArray:statisticsList];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    } balanceID:self.ID];
}

- (void)reload:(__unused id)sender {
    JFBLoadingAnimation * animation = [[JFBLoadingAnimation alloc] init:self.view];
    [JFBSettlementDetailsDS loadSettlementDetail:^(NSArray *statisticsList, NSError *error) {
        if (!error) {
            [self.items addObjectsFromArray:statisticsList];
            [animation stop];
            [self.tableView reloadData];
        }
    } balanceID:self.ID];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.items count]==0) {
        [self.tableView setSeparatorStyle: UITableViewCellSeparatorStyleNone];
    }else{
        [self.tableView setSeparatorStyle: UITableViewCellSeparatorStyleSingleLine];
    }
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettlementDetailsCell *cell = (SettlementDetailsCell*)[tableView dequeueReusableCellWithIdentifier:@"settlementDetailsCell" forIndexPath:indexPath];
    JFBSettlementDetailsDS *ds = self.items[indexPath.row];
    cell.settlementDetails = ds;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


-(UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    return item;
}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
